package org.jeecg.desen.run.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.desen.run.component.DscConversionService;
import org.jeecg.desen.run.component.hive.DscHiveConversionService;
import org.jeecg.desen.run.entity.DscSsc;
import org.jeecg.desen.run.entity.DscSscFieldDetail;
import org.jeecg.desen.run.entity.DscSscTableDetail;
import org.jeecg.desen.run.service.DesAutoRunService;
import org.jeecg.modules.meta.entity.SysFieldinfo;
import org.jeecg.modules.meta.entity.SysTableinfo;
import org.jeecg.modules.meta.service.ISysFieldinfoService;
import org.jeecg.modules.meta.service.ISysTableinfoService;
import org.jeecg.modules.rule.entity.Autossc;
import org.jeecg.modules.rule.entity.AutosscDetail;
import org.jeecg.modules.rule.service.IAutosscDetailService;
import org.jeecg.modules.rule.service.IAutosscService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("desRunAutoDayService")
public class DesRunAutoService extends AbstractDesRunService implements DesAutoRunService {

    private String proNum;

    @Autowired
    IAutosscService iAutosscService;

    @Autowired
    IAutosscDetailService iAutosscDetailService;

    @Resource(name = "dscAutoHiveConversionServiceImpl")
    DscHiveConversionService dscConversionService;

    @Autowired
    ISysFieldinfoService iSysFieldinfoService;

    @Autowired
    ISysTableinfoService iSysTableinfoService;

    @Override
    public DesRunAutoService bindProNum(String pro_num, Date runDate) {
        log.info("自动脱敏bind参数,频次=" + pro_num);
        proNum = pro_num;
        this.runDate = runDate;
        return this;
    }

    @Override
    DscSsc getRunDscSsc(String sscId) {
        Autossc autossc = getAutossc(sscId);
        DscSsc dscSsc = getDscSscEntity(autossc);
        dscSsc.setTableDetailList(convSscDetail(getAutoSscListDetail(dscSsc.getSscid()),dscSsc));
        return dscSsc;
    }

    @Override
    List<DscSsc> getDscSsc() {
        //获取频次对应的订阅信息 ，当个频次可以对应
        List<Autossc> list = getAutosscList(proNum);
        //获取订阅相关的表信息
        List<DscSsc> dscSscs = countDscSscs(list);
        //进行自动订阅表信息查询
        sscQueryAutoTable(dscSscs);
        return dscSscs;
    }

    @Override
    DscConversionService getComponent() {
        return dscConversionService.bindParam(proNum, runDate);
    }

    @Override
    void setTableField(DscSscTableDetail dscSscTableDetail) {
        //根据表id获取field字段集合
        String tableId = dscSscTableDetail.getFdTableId();
        dscSscTableDetail.setFieldDetailList(convSscFieldDetail(getFieldList(tableId)));
    }

    @Override
    void setWhere(DscSscTableDetail dscSscTableDetail) {
        String tableId = dscSscTableDetail.getFdTableId();
        SysTableinfo sysTableinfo = getSysTableinfo(tableId);
        if (null != sysTableinfo) {
            dscSscTableDetail.setFdLoaddt(sysTableinfo.getFdLoaddt());
            dscSscTableDetail.setFdLoaddt2(sysTableinfo.getFdPartition2());
            dscSscTableDetail.setFdType(sysTableinfo.getFdType());
            dscSscTableDetail.setFdTableName(sysTableinfo.getFdTablename());
        }
    }

    List<SysFieldinfo> getFieldList(String tableId) {
        QueryWrapper<SysFieldinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fd_tableid", tableId);
        queryWrapper.notIn("fd_status", "2");
        return iSysFieldinfoService.list(queryWrapper);
    }

    private void sscQueryAutoTable(List<DscSsc> dscSscs) {
        for (DscSsc dscSsc : dscSscs) {
            dscSsc.setTableDetailList(convSscDetail(getAutoSscListDetail(dscSsc.getSscid()),dscSsc));
        }
    }

    private List<DscSsc> countDscSscs(List<Autossc> autosscs) {
        if (null == autosscs || autosscs.size() == 0) {
            return new ArrayList<>();
        }

        List<DscSsc> dscSscs = new ArrayList<>();
        for (Autossc autossc : autosscs) {
            dscSscs.add(getDscSscEntity(autossc));
        }
        return dscSscs;
    }

    private DscSsc getDscSscEntity(Autossc autossc) {
        DscSsc dscSsc = new DscSsc();
        dscSsc.setSscid(autossc.getId());//订阅id
        dscSsc.setSscName(autossc.getFdName());//订阅名称
        dscSsc.setSscType(1);//类型是否是自动
        dscSsc.setFdProNum(proNum);//类型是否是自动
        dscSsc.setExpain(autossc.getFdExpain());//说明
        dscSsc.setFdProject(autossc.getFdProject());//应用
        return dscSsc;
    }

    private List<Autossc> getAutosscList(String proNum) {
        //获取频次对应的订阅信息 ，当个频次可以对应
        QueryWrapper<Autossc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fd_pro_num", proNum);
        queryWrapper.eq("fd_status", 1);
        return iAutosscService.list(queryWrapper);
    }

    private Autossc getAutossc(String sscId) {
        return iAutosscService.getById(sscId);
    }



    private List<DscSscFieldDetail> convSscFieldDetail(List<SysFieldinfo> sysFieldinfos) {
        List<DscSscFieldDetail> dscSscTableDetails = new ArrayList<>();
        for (SysFieldinfo sysFieldinfo : sysFieldinfos) {
            DscSscFieldDetail dscSscFieldDetail = new DscSscFieldDetail();
            dscSscFieldDetail.setFdFieldid(sysFieldinfo.getFdFieldid());
            dscSscFieldDetail.setFdFieldname(sysFieldinfo.getFdFieldname());
            dscSscFieldDetail.setFdRulelevel(sysFieldinfo.getFdRulelevel());
            dscSscFieldDetail.setFdRulename(sysFieldinfo.getFdRulename());
            dscSscFieldDetail.setFdDes("1");
            dscSscTableDetails.add(dscSscFieldDetail);
        }
        return dscSscTableDetails;
    }

    private List<DscSscTableDetail> convSscDetail(List<AutosscDetail> autosscDetails,DscSsc dscSsc) {
        List<DscSscTableDetail> dscSscTableDetails = new ArrayList<>();
        for (AutosscDetail autosscDetail : autosscDetails) {
            DscSscTableDetail dscSscTableDetail = new DscSscTableDetail();
            dscSscTableDetail.setFdTableId(autosscDetail.getFdTableId());
            dscSscTableDetail.setFdWhere(autosscDetail.getFdWhere());
            dscSscTableDetail.setSscid(autosscDetail.getFdSscid());
            dscSscTableDetail.setFdSource(dscSsc.getFdSource());
            dscSscTableDetails.add(dscSscTableDetail);
        }
        return dscSscTableDetails;
    }

    private List<AutosscDetail> getAutoSscListDetail(String sscid) {
        QueryWrapper<AutosscDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fd_sscid", sscid);
        queryWrapper.notIn("fd_status", 2);
        return iAutosscDetailService.list(queryWrapper);
    }

    @Override
    boolean verificationParam() {
        if (StringUtils.isEmpty(proNum)) {
            return false;
        }
        return true;
    }

    @Override
    String getParam() {
        return "自动脱敏参数,proNum=" + proNum + ",runDate=" +
                (null != runDate ? DateUtil.formatDate(runDate) : "") + ",endDate=" +
                (null != endDate ? DateUtil.formatDate(endDate) : "");
    }
}
